package com.kehutong.payfees.util;

import com.kehutong.common.urlconfig.UrlConfig;
import org.coraframework.boot.Application;
import org.coraframework.config.Config;
import org.coraframework.http.HttpClient;
import org.coraframework.http.HttpImpl;
import org.coraframework.inject.Singleton;
import org.coraframework.json.JSON;
import org.coraframework.json.JSONObject;
import org.coraframework.util.CallBack;

import java.io.IOException;

/**
 * e签宝相关配置
 *
 */
@Singleton
public class ESignTreasureUrlConfig extends UrlConfig {


    private final Config config = Application.getConfig("eSignTreasure");

    public final String appId = config.getString("appId");

    public final String appSecret = config.getString("appSecret");

    public final String rsaPublicKey = config.getString("rsaPublicKey");

    protected ESignTreasureUrlConfig() {
        super("eSignTreasure", "web_url");
    }

    public JSONObject put(String page, CallBack<HttpClient> clientCallBack) throws Exception {
        final HttpImpl httpClient = new HttpImpl(getUrl() + page) {
            @Override
            protected String fetch(String method) throws IOException {
                return super.fetch("PUT");
            }
        };
        clientCallBack.call(httpClient);

        String text = httpClient.post();
        final JSONObject rs = JSON.parseObject(text);

        if (rs.getInteger("code") == 0) {
            return rs.getJSONObject("data");
        }

        if (rs.getInteger("code") == 401) {
            throw new ESignTreasureRequestFailException("请求失败！");
        }
        throw new RuntimeException(rs.getString("message"));
    }

    public JSONObject delete(String page, CallBack<HttpClient> clientCallBack) throws Exception {
        final HttpImpl httpClient = new HttpImpl(getUrl() + page) {
            @Override
            protected String fetch(String method) throws IOException {
                return super.fetch("DELETE");
            }
        };
        clientCallBack.call(httpClient);

        String text = httpClient.post();
        final JSONObject rs = JSON.parseObject(text);

        if (rs.getInteger("code") == 0) {
            return rs.getJSONObject("data");
        }

        if (rs.getInteger("code") == 401) {
            throw new ESignTreasureRequestFailException("请求失败！");
        }
        throw new RuntimeException(rs.getString("message"));
    }

    public JSONObject postESignTreasure(String page, CallBack<HttpClient> clientCallBack) throws Exception {
        final HttpClient httpClient = HttpClient.create(getUrl() + page);
        clientCallBack.call(httpClient);

        String text = httpClient.post();
        final JSONObject rs = JSON.parseObject(text);

        if (rs.getInteger("code") == 0) {
            return rs.getJSONObject("data");
        }

        if (rs.getInteger("code") == 401) {
            throw new ESignTreasureRequestFailException("请求失败！");
        }
        throw new RuntimeException(rs.getString("message"));
    }

    public JSONObject getESignTreasure(String page, CallBack<HttpClient> clientCallBack) throws Exception {
        final HttpClient httpClient = HttpClient.create(getUrl() + page);
        clientCallBack.call(httpClient);

        String text = httpClient.get();
        final JSONObject rs = JSON.parseObject(text);

        if (rs.getInteger("code") == 0) {
            return rs.getJSONObject("data");
        }

        if (rs.getInteger("code") == 401) {
            throw new ESignTreasureRequestFailException("请求失败！");
        }

        throw new RuntimeException(rs.getString("message"));
    }

    public void upload(String page, CallBack<HttpClient> clientCallBack) throws Exception {
        final HttpImpl httpClient = new HttpImpl(page) {
            @Override
            protected String fetch(String method) throws IOException {
                return super.fetch("PUT");
            }
        };
        clientCallBack.call(httpClient);

        String text = httpClient.post();
        System.out.println(text);
//        final JSONObject rs = JSON.parseObject(text);
//
//        if (rs.getInteger("code") == 0) {
//            return rs.getJSONObject("data");
//        }
//
//        if (rs.getInteger("code") == 401) {
//            throw new ESignTreasureRequestFailException("请求失败！");
//        }
//        throw new RuntimeException(rs.getString("message"));
    }
}
